Repository navigation
Treat an argument with no known refinement as unconstrained - #496
Merged
Merged
Conversation
An argument whose refinement is just `true` (e.g. the result of a method without refinements) was turned into `arg == true`. For an int argument that crashed with "Sorts Int and Bool are incompatible"; for a boolean one it wrongly assumed the value was true. Fixes #451 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
Author
|
Codex review, round 1
Fixed in 54a6de7: the equality is now skipped only when the argument's refinement is exactly the literal |
Collaborator
Author
|
Codex review, round 2 No findings: "The change prevents an unknown argument refinement from being treated as the literal value |
Collaborator
Author
Clean-code review (clean-code-principles)Reviewed the diff only, weighed against the standing rules (smallest PR that tests well, match surrounding style, no tidy-only refactors, lean tests). Nothing worth changing; no new commits.
|
CatarinaGamboa
added a commit
that referenced
this pull request
Nov 23, 2026
Fixes #448 ## Root cause `RefinementTypeChecker.visitCtNewArray` builds the `length(_) == n` refinement of a new array only from its size expressions (`new byte[n]`). An array initializer (`new byte[] {1, 2}`, or `{...}` in a declaration) has no size expression, so the new array got no refinement at all and its length was unknown. When the issue was filed, the argument with no refinement was wrapped as `arg == true`, which Z3 rejected (`Sorts |byte[]| and Bool are incompatible`). Since #496 (an argument with no known refinement is unconstrained), the crash is gone, but the correct call is still rejected because nothing is known about `length(b)`. ## Fix When a `CtNewArray` has no size expressions, its refinement is `length(_) == <number of elements>`. This covers every element type, nested initializers (the outer length is the number of rows), and empty initializers (`new int[] {}`, `int[] e = {};`). ## Before / after ```java static void put(byte[] b, @refinement("_ >= 0 && _ <= length(b)") int n) { } put(new byte[] {1, 2}, 2); put(new byte[] {1, 2}, 3); ``` | call | main (e3b11c8) | this PR | |---|---|---| | `put(new byte[] {1, 2}, 2)` | `Refinement Error: found n == 2 but expected n >= 0 && n <= length(b)`, counterexample `length(b) == 0` | `Correct! Passed Verification.` | | `put(new byte[] {1, 2}, 3)` | same error, counterexample `length(b) == 0` | `Refinement Error: found n == 3 && length(b) == 2 but expected n >= 0 && n <= length(b)` | I also checked these by hand: `int[]`, `String[]`, `int[][] {{1}, {2, 3}}` (length 2), `new int[] {}`, `int[] e = {}`, and `@Refinement("length(_) == 2") int[] f = {4, 5}` all pass. The same calls with a wrong length give a Refinement Error. ## Test I extended `CorrectStringArrayLength` with one call that passes an initializer and one that passes an empty declaration initializer, both against an exact `_ == length(names)` refinement. The fixture fails on main. I added no Error fixture, because main already reports an error for a wrong length (only for the wrong reason), so such a fixture would pass without the fix. The exact `==` refinement already catches a wrong element count. The full `mvn test` passes locally. ## Relation to #400 #400 (`.length` on array expressions, spread varargs) gives a spread varargs call a fresh array with `length == number of arguments`. This PR handles explicit initializers, the case #400's `count(new String[] { "x" })` passes through the normal argument path. The two PRs touch different methods and do not conflict. ## Not changed here `new int[2][3]` gets `length == 3`, because the loop over size expressions keeps the last one (the existing `TODO only working for 1 dimension`). So `grid(new int[2][3], 3)` against `_ == length(b)` is wrongly accepted. This bug is older than this PR and is not part of it. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #451
Cause. When a call argument carries no information (its refinement is just
true, as for the result of a method without refinements),createVariableRepresentingArgumentstill wrapped it asarg == true. For anintargument Z3 rejects that with "Sorts Int and Bool are incompatible". For abooleanargument it silently assumed the value wastrue, sotakeTrue(s.isEmpty())against@Refinement("_ == true")was accepted. Now such an argument is left unconstrained, like any unknown value.Study exercise A2_04, fixed with
socket.connect(addr, Math.max(timeout, 0))(no spec forMath.max):Before:
After:
The other A2_04 fixes (guard + return, guard + throw, ternary) still verify. With a
Math.maxspec (_ >= a && _ >= b && (_ == a || _ == b)) theMath.maxfix verifies too. The issue'snew Socket(addr, server.getLocalPort())now reportsexpected server.getLocalPort() >= 0 && server.getLocalPort() <= 65535 but could not prove it.Tests. Two cases added to
ErrorUnknownMethodInOperation: an unknownintresult and an unknownbooleanresult passed as arguments. Both fail on main (the first crashes, the second is accepted).🤖 Generated with Claude Code